<?php

namespace App\Http\Controllers\Api\Members;

use App\Http\Controllers\Controller;
use App\Libs\Strategy\Context;
use App\Models\Members;
use Illuminate\Http\Request;

class MembersGroupController extends Controller
{
    private $member;

    public function __construct()
    {
        $this->member = new Context(new Members());
    }

    public function show($id)
    {
        $members = $this->member->display(request()->limit,function($query) use($id){
            $query->where('pid',$id);
            $query->with(['roomsGoods' => function($buys){
                $buys->with('room_goods');
                return $buys;
            }]);
             if(request()->keyword)
            {
                $query->where('mobile','like','%'.request()->keyword.'%');
            }
            if(isset(request()->time[0]) && !empty(request()->time[0])){
                $query->whereBetween('created_at', request()->time);
            }
            return $query;
        });
        $beans_count = $this->member->display(request()->limit,function($query) use($id){
            $query->where('pid',$id);
            $query->with(['roomsGoods' => function($buys){
                $buys->with('room_goods');
                return $buys;
            }]);
            return $query;
        })->sum('bean');
        $members->each(function($item){
            $item->room_num = $item['roomsGoods']['room_goods']['room_num'];
            return $item;
        });
        return $this->success([
            'group_flow' => $members,
            'beans_count' => $beans_count
        ]);
    }

}
